package com.youshang520i.zookeeper.demo;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.proto.WatcherEvent;

import java.io.IOException;

/**
 *  监听单节点内容
 */
public class WatchDemo {

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
        ZooKeeper zkCli = new ZooKeeper("192.168.3.123:2181,192.168.3.123:2182,192.168.3.123:2183", 3000,
                new Watcher() {
                    // 监听回调
                    @Override
                    public void process(WatchedEvent event) {
                        System.out.println("监听回调！！！！");
                    }
                });
        String path = "/hello";
        byte[] data = zkCli.getData(path, new Watcher() {
            // 监听的具体内容
            @Override
            public void process(WatchedEvent event) {
                System.out.println("监听路径为：" + event.getPath());
                System.out.println("监听的类型为：" + event.getType());
                System.out.println("当前事件状态："+  event.getState());
                System.out.println("监听被修改了！！！");
            }
        }, null);
        System.out.println("当前节点("+path+")的值为:" + new String(data));
        Thread.sleep(Long.MAX_VALUE);
    }
}
